package com.koala.learn.beanlearn;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.koala.learn.beanlearn.entity.Person;
import com.koala.learn.beanlearn.entity.PersonVO;
import com.koala.learn.beanlearn.entity.Student;
import com.koala.learn.beanlearn.entity.StudentVo;
import com.koala.utils.OrikaUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * orika 学习
 *
 * @author yantingrui
 * @date 2021/12/9   20:47
 */
public class Start {

    public static void main(String[] args) {
        Integer count = 12;
        Person person = new Person();
        Optional.ofNullable(count).ifPresent(v -> {
            person.setAge(v);
        });
        System.out.println(person);
    }


    /**
     * 测试copyProperties 是用的get 还是set
     */
    @Test
    public void test26() {
        Person person = new Person();
        person.setAge(11);
        person.setDate(LocalDateTime.now());
        person.setName("Koala");
        PersonVO personVO = new PersonVO();
        BeanUtils.copyProperties(person, personVO);
        System.out.println(personVO.toString());

    }

    /**
     * 测试OrikaUtil.map 是get 还是 set
     * 结论:
     */
    @Test
    public void test41() {
        Person person = new Person();
        person.setAge(11);
        person.setDate(LocalDateTime.now());
        person.setName("Koala");
        OrikaUtil.map(person, OrikaUtil.getType(Person.class), OrikaUtil.getType(PersonVO.class));
    }

    /**
     * mapList
     * 一种是复写Person的get方法
     * 另一种是 在PersonVO 加一个字段  xxxStr 复写xxx 的setxxx 方法
     * 不能取同一个字段名 复制
     */
    @Test
    public void test59() {
        ArrayList<Person> peoples = new ArrayList<>();
        Person p0 = new Person("Tik", 12, LocalDateTime.now());
        Person p1 = new Person("Tik", 12, LocalDateTime.now());
        Person p2 = new Person("Tik", 12, LocalDateTime.now());
        peoples.add(p2);
        peoples.add(p0);
        peoples.add(p1);
        List<PersonVO> personVOS = OrikaUtil.mapList(peoples, Person.class, PersonVO.class);
        System.out.println(JSON.toJSONString(personVOS));
    }

    /**
     *
     */
    @Test
    public void test() {
        //
        ArrayList<Student> personArrayList = CollectionUtil.newArrayList();
        OrikaUtil.mapList(personArrayList, Student.class, StudentVo.class);
    }

    /**
     *
     */
    @Test
    public void test2() {
        Person person = new Person("Tik", 12);
        //Function<Integer, String> function = Object::toString+"12";
        //String fun = fun(function, 123);
        //System.out.println(fun);
    }

    public <T, K> K fun(Function<T, K> method, T value) {
        return method.apply(value);
    }

    /**
     *
     */
    @Test
    public static void test103() {

    }

}
